package jun.learn.xmlprocesor.sax;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;


public class SaxParserXml extends DefaultHandler {
	private List<Student> list;
	
	private Student student;
	
	private String tagName;
	
	public String getTagName() {
		return tagName;
	}

	public void setTagName(String tagName) {
		this.tagName = tagName;
	}

	@Override
	public void startDocument() throws SAXException {
		list = new ArrayList<Student>();
	}
	
	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		if(qName.equals("student")){
			student = new Student();
			student.setId(Integer.parseInt(attributes.getValue(0)));
			student.setGroup(Integer.parseInt(attributes.getValue(1)));
		}
		setTagName(qName);
	}
	
	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if(qName.equals("student")){
			getList().add(getStudent());
		}
		this.tagName = null;
	}
	
	@Override
	public void endDocument() throws SAXException {
		super.endDocument();
	}
	
	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		if(this.tagName!=null){
            String date=new String(ch,start,length);
            if(this.tagName.equals("name")){
                this.student.setName(date);
            }
            else if(this.tagName.equals("sex")){
                this.student.setSex(date);
            }
            else if(this.tagName.equals("age")){
                this.student.setAge(Integer.parseInt(date));
            }
            else if(this.tagName.equals("email")){
                this.student.setEmail(date);
            }
            else if(this.tagName.equals("birthday")){
                this.student.setBirthday(date);
            }
            else if(this.tagName.equals("memo")){
                this.student.setMemo(date);
            }
        }
	}
	
	public static void main(String[] args) {
		SAXParser parser = null;
		
		try {
			SaxParserXml parserXml = new SaxParserXml();
			parser = SAXParserFactory.newInstance().newSAXParser();
			InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("jun/learn/xmlprocesor/sax/persons.xml");
			System.out.println(is);
			parser.parse(is, parserXml);
			List<Student> list = parserXml.getList();
			for(Student s : list){
				System.out.println(s);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public List<Student> getList() {
		return list;
	}

	public void setList(List<Student> list) {
		this.list = list;
	}

	public Student getStudent() {
		return student;
	}

	public void setStudent(Student student) {
		this.student = student;
	}
	
}
